package ru.mcfr.oxygen.framework.operations;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.node.AttrValue;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;

import javax.swing.text.BadLocationException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ws
 * Date: 4/18/11
 * Time: 11:12 AM
 * To change this template use File | Settings | File Templates.
 */
public class InsertAuthorBlockOperation implements AuthorOperation{
    public void doOperation(AuthorAccess authorAccess, ArgumentsMap argumentsMap) throws IllegalArgumentException, AuthorOperationException {
        AuthorDocumentController controller = authorAccess.getDocumentController();

        String blockType = "";
        try {
            blockType = argumentsMap.getArgumentValue("тип вреза").toString();
        } catch (Exception e) {e.printStackTrace();}

        String operationType = "";
        try {
            operationType = argumentsMap.getArgumentValue("тип операции").toString();
        } catch (Exception e) {e.printStackTrace();}

        controller.beginCompoundEdit();

        if (operationType.equalsIgnoreCase("insert")){
            String xmlFragment = "<врез тип_вреза=\"" + blockType + "\"/>";
            if (authorAccess.getEditorAccess().hasSelection())
                controller.surroundInFragment(xmlFragment,authorAccess.getEditorAccess().getSelectionStart(),authorAccess.getEditorAccess().getSelectionEnd());
            else
                controller.insertXMLFragmentSchemaAware(xmlFragment,authorAccess.getEditorAccess().getCaretOffset());
        } else if (operationType.equalsIgnoreCase("change")){
            //select needed element
            try {
                AuthorNode selectedNode = controller.getNodeAtOffset(authorAccess.getEditorAccess().getCaretOffset());
                while (!selectedNode.getName().equalsIgnoreCase("врез"))
                    selectedNode = selectedNode.getParent();
                controller.setAttribute("тип_вреза", new AttrValue(blockType), (AuthorElement) selectedNode);

            } catch (BadLocationException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (operationType.equalsIgnoreCase("delete")){
            authorAccess.getWorkspaceAccess().showErrorMessage("delete");
            try {
                int offset = authorAccess.getEditorAccess().getCaretOffset();
                AuthorNode selectedNode = controller.getNodeAtOffset(offset);
                while (!selectedNode.getName().equalsIgnoreCase("врез"))
                    selectedNode = selectedNode.getParent();
                if (selectedNode.getType() == AuthorNode.NODE_TYPE_ELEMENT & selectedNode.getName().equalsIgnoreCase("врез")){
                    List<AuthorNode> content = ((AuthorElement) selectedNode).getContentNodes();
                    int startOffset = content.get(0).getStartOffset();
                    int endOffset = content.get(content.size() - 1).getEndOffset();
                    AuthorDocumentFragment fragment = controller.createDocumentFragment(startOffset, endOffset);
                    controller.deleteNode(selectedNode);
                    controller.insertFragmentSchemaAware(offset, fragment);
                }

            } catch (BadLocationException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        } else if (operationType.equalsIgnoreCase("change")){
            try {
                AuthorNode selectedNode = controller.getNodeAtOffset(authorAccess.getEditorAccess().getCaretOffset());
                while (!selectedNode.getName().equalsIgnoreCase("врез"))
                    selectedNode = selectedNode.getParent();

                controller.setAttribute("тип вреза", new AttrValue(blockType), (AuthorElement) selectedNode);
            } catch (BadLocationException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        controller.endCompoundEdit();

    }

    public ArgumentDescriptor[] getArguments() {
        ArgumentDescriptor[] res = {
                new ArgumentDescriptor("тип вреза", ArgumentDescriptor.TYPE_STRING, ""),
                new ArgumentDescriptor("тип операции", ArgumentDescriptor.TYPE_STRING, "insert/change/delete"),
        };
        return res;
    }

    public String getDescription() {
        return "Вставка блока типа _врез_";
    }
}
